package com.have.blog.admin.domain.bo;

import com.have.blog.base.constant.BaseConstant;
import com.have.blog.model.models.Admin;
import com.have.blog.model.models.ResourceMenu;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class AdminUserDetail implements UserDetails {
    private Admin admin;
    private List<ResourceMenu> resourceList;
    private String roleName;

    public AdminUserDetail(Admin admin, String roleName, List<ResourceMenu> resourceList) {
        this.admin = admin;
        this.resourceList = resourceList;
        this.roleName = roleName;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return resourceList.stream()
                .map(resource -> new SimpleGrantedAuthority(resource.getName() + BaseConstant.SYMBOL_COLON + resource.getSummary()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return admin.getPassword();
    }

    @Override
    public String getUsername() {
        return admin.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public Admin getAdmin() {
        return admin;
    }

    public String getRoleName() {
        return roleName;
    }
}
